// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/service/module/blog_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/blog"
	blogModel "gin-sns/app/model/module/blog"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var BlogService = newBlogService()

func newBlogService() *blogService {
	return &blogService{}
}

type blogService struct {
}

//根据主键查询数据
func (s *blogService) SelectRecordById(id int64) (*blogModel.Entity, error) {
	return blog.BlogDao.SelectById(id)
}

//
func (s *blogService) SelectRecordsIn(idarr []int64) ([]blogModel.Entity, error) {

	list, err := blog.BlogDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *blogService) DeleteRecordById(id int64) bool {
	return blog.BlogDao.DeleteById(id)
}

//批量删除数据记录
func (s *blogService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := blog.BlogDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *blogService) AddSave(req *blogModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity blogModel.Entity

	//entity.Id = req.Id
	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.CountNum = req.CountNum
	entity.CountView = req.CountView
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := blog.BlogDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *blogService) EditSave(req *blogModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &blogModel.Entity{Id: req.Id}
	ok, err := blog.BlogDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.CountNum = req.CountNum
	entity.CountView = req.CountView
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return blog.BlogDao.Update(entity)
}

//根据条件查询数据
func (s *blogService) SelectListAll(params *blogModel.SelectPageReq) ([]blogModel.Entity, error) {
	return blog.BlogDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *blogService) SelectListByPage(params *blogModel.SelectPageReq) ([]blogModel.Entity, *page.Paging, error) {
	return blog.BlogDao.SelectListByPage(params)
}

// 导出excel
func (s *blogService) Export(param *blogModel.SelectPageReq) (string, error) {
	head := []string{"博客id", "用户id", "博客名称", "博客简介", "博客图片logo", "文章数量", "阅读数量", "被关注数量", "0未审核 1正常状态 2审核未通过 3删除", "创建时间"}
	col := []string{"id", "user_id", "title", "description", "logo", "count_num", "count_view", "count_follow", "status", "create_time"}
	return blog.BlogDao.SelectListExport(param, head, col)
}

/*
// 分析文章
func AnalyzeArticle(article *articleModel.Entity) {
	blogIds := blog.AnalyzeBlogs(article.UserId, article.Title, article.Content)
	if len(blogIds) > 0 {
		for _, blogId := range blogIds {
			summary := article.Summary
			if summary == "" {
				summary = common.GetSummary(article.ContentType, article.Content)
			}
			_, err := Publish(blogId,constants.EntityTypeArticle, article.Id,
				article.Title, summary)
			if err != nil {
				logrus.Error(err)
			}
		}
	}
}

// 发布
func Publish(blogId int64, entityType int, entityId int64, title, summary string) (c *blogModel.Entity, err error) {
	c = GetByEntity(entityType, entityId)
	if c != nil {
		c.BlogId = blogId
		c.EntityType = entityType
		c.EntityId = entityId
		c.Title = title
		c.Summary = summary
		c.Deleted = false
		c.CreateTime = time.Now()
		err = blogModel.Update( c)
	} else {
		c := &blogModel.Entity{
			BlogId:     blogId,
			EntityType: entityType,
			EntityId:   entityId,
			Title:      title,
			Summary:    summary,
			Deleted:    false,
			CreateTime: time.Now(),
		}
		err = blogModel.Create( c)
	}
	return
}
*/
